require '../src/profile'
require 'test/unit'


class TestProfile < Test::Unit::TestCase
	def teardown
		%x{del /f /q /s coverage 2>nul}
		%x{rd /q /s coverage 2>nul}
	end
	def test_task_info_normal
		argv = ["x.exe", "y.dll"]
		info = QCov::Profile::TaskInfo.new argv

		assert_equal info.name, 'y'
		assert_equal info.exe_name, 'x.exe'
		assert_equal info.modules[0], 'y.dll'
		assert_equal info.dir, ''
		assert_equal info.file, 'y'
	end

	def test_task_info_relpath
		argv = ["../test/x.exe", "../test/y.dll"]
		info = QCov::Profile::TaskInfo.new argv

		assert_equal info.name, '../test/y'
		assert_equal info.exe_name, '../test/x.exe'
		assert_equal info.modules[0], '../test/y.dll'
		assert_equal info.dir, '../test/'
		assert_equal info.file, 'y'
	end

	def test_task_info_omit_ext
		argv = ["../test/x.exe", "../test/y"]
		info = QCov::Profile::TaskInfo.new argv

		assert_equal info.name, '../test/y'
		assert_equal info.exe_name, '../test/x.exe'
		assert_equal info.modules[0], '../test/y.dll'
		assert_equal info.dir, '../test/'
		assert_equal info.file, 'y'
	end

	def test_task_info_omit_exe
		argv = ["../test/x", "../test/y"]
		info = QCov::Profile::TaskInfo.new argv

		assert_equal info.name, '../test/y'
		assert_equal info.exe_name, '../test/x.exe'
		assert_equal info.modules[0], '../test/y.dll'
		assert_equal info.dir, '../test/'
		assert_equal info.file, 'y'
	end

	def test_task_info_only_exe
		argv = ["../test/x"]
		info = QCov::Profile::TaskInfo.new argv

		assert_equal info.name, '../test/x'
		assert_equal info.exe_name, '../test/x.exe'
		assert_equal info.modules[0], '../test/x.exe'
		assert_equal info.dir, '../test/'
		assert_equal info.file, 'x'
	end
	
	def test_task_info_bat
		argv = ["../test/x.bat"]
		assert_raise QCov::Profile::FatalError do
			QCov::Profile::TaskInfo.new argv
		end

		argv = ["../test/x.bat", "../test/y"]
		info = QCov::Profile::TaskInfo.new argv
		assert_equal '../test/x.bat', info.exe_name
	end
	
	def test_task_info_lots_of_mods
		argv = ["x", "x.exe", "y", "y", "y", "y", "y", "y", "y"]
		info = QCov::Profile::TaskInfo.new argv
		assert_equal info.name, 'x'
		assert_equal info.exe_name, 'x.exe'
		assert_equal info.dir, ''
		assert_equal info.file, 'x'
		assert_equal info.modules.size, 8
		assert_equal info.modules[0], 'x.exe' 
		assert_equal info.modules[1], 'y.dll'
	end

	def test_cmd_pre_prep
		argv = ["../test/x", "../test/y"]
		info = QCov::Profile::TaskInfo.new argv
		assert_equal info.cmd_pre_prep, "prep /lv /om ../test/y.dll"
	end

	def test_cmd_profile
		argv = ["../test/x", "../test/y"]
		info = QCov::Profile::TaskInfo.new argv
		assert_equal info.cmd_profile, "profile /i ../test/y /o ../test/y ../test/x.exe"
	end

	def test_cmd_post_prep
		argv = ["../test/x", "../test/y"]
		info = QCov::Profile::TaskInfo.new argv
		assert_equal info.cmd_post_prep, "prep /m ../test/y"
	end

	def test_cmd_plist
		argv = ["../test/x", "../test/y"]
		info = QCov::Profile::TaskInfo.new argv
		assert_equal info.cmd_plist, "plist ../test/y"
	end
	
	def test_pass_dir_as_args
		argv = ["../test", "../test/y"]
		assert_raise QCov::Profile::FatalError do
			QCov::Profile::TaskInfo.new argv
		end
		
		argv = ["../test/", "../test/y"]
		assert_raise QCov::Profile::FatalError do
			QCov::Profile::TaskInfo.new argv
		end
		
		argv = ["../test/x", "../test"]
		assert_raise QCov::Profile::FatalError do
			QCov::Profile::TaskInfo.new argv
		end
		
	end
	
	def test_guarded_exec
		assert_raise QCov::Profile::FatalError do
			QCov::Profile::helper_guarded_exec "nosuchcmd"
		end
	end
	
	def test_parse_callback
		excludes = [/VC98\\Include/i, /Coding\\include\\boost/i, /Coding\\include\\stl/i, /stdafx/i]
		
		stat = QCov::Profile::parse 'sample/test1.profile', excludes do |file|
			assert_equal 'test1.cpp', File.basename(file[:name])
			assert_equal 'test1.exe', file[:module]
			
			assert_equal 16, file[:total]
			assert_equal 6, file[:covered]
			assert_equal 0, file[:uncovered]
			assert_equal 10, file[:noop]
			
			assert_equal "    printf(\"Hello World!\\n\");", file[:lines][11][:text]
			assert_equal RCova::Covered, file[:lines][11][:status]
		end
	end

	def test_parse_stat
		excludes = [/VC98\\Include/i, /Coding\\include\\boost/i, /Coding\\include\\stl/i, /stdafx/i]
		stat = QCov::Profile::parse 'sample/test1.profile', excludes

		assert_equal 10000, stat[:coverage]
		assert_equal 16, 	stat[:total]
		assert_equal 1, 	stat[:modules].size

		assert_equal "test1.exe", stat[:modules][0][:name]
		assert_equal 10000, stat[:modules][0][:coverage]
		assert_equal 16, 	stat[:modules][0][:total]
		assert_equal 1, 	stat[:modules][0][:files].size

		assert_equal "test1.cpp", File.basename(stat[:modules][0][:files][0][:name])
		assert_equal 16, 	stat[:modules][0][:files][0][:total]
		assert_equal 6, 	stat[:modules][0][:files][0][:covered]
		assert_equal 0, 	stat[:modules][0][:files][0][:uncovered]
		assert_equal 10, 	stat[:modules][0][:files][0][:noop]

	end

	def test_parse_stat_66percent
		excludes = [/VC98\\Include/i, /Coding\\include\\boost/i, /Coding\\include\\stl/i, /stdafx/i]
		stat = QCov::Profile::parse 'sample/test1.profile.66percent', excludes

		assert_equal 6666, stat[:coverage]
		assert_equal 16, 	stat[:total]
		assert_equal 1, 	stat[:modules].size

		assert_equal "test1.exe", stat[:modules][0][:name]
		assert_equal 6666, stat[:modules][0][:coverage]
		assert_equal 16, 	stat[:modules][0][:total]
		assert_equal 1, 	stat[:modules][0][:files].size

		assert_equal "test1.cpp", File.basename(stat[:modules][0][:files][0][:name])
		assert_equal 16, 	stat[:modules][0][:files][0][:total]
		assert_equal 6, 	stat[:modules][0][:files][0][:covered]
		assert_equal 3, 	stat[:modules][0][:files][0][:uncovered]
		assert_equal 7, 	stat[:modules][0][:files][0][:noop]

	end
	
	def test_convert_path_to_filename
		ret = QCov::Profile::helper_convert_path_to_filename(
			"f:\\home\\etc\\winsys\\desktop\\kcov\\test\\test1\\test1.cpp")
		assert_equal "files/home-etc-winsys-desktop-kcov-test-test1-test1_cpp.html", ret
	end
	
	def test_html_report_spliter
        erb_index = ERB.new(open('..\src\index.rhtml') {|io| io.read }, nil, '-')
        erb_file = ERB.new(open('..\src\file.rhtml') {|io| io.read }, nil, '-')
		excludes = [/VC98\\Include/i, /Coding\\include\\boost/i, /Coding\\include\\stl/i, /stdafx/i]
		QCov::Profile::html_report 'sample/test1.profile', excludes, erb_index, erb_file
		assert File.exist?('coverage/files/home-etc-winsys-desktop-kcov-test-test1-test1_cpp.html')
	end
	
	def test_html_report
        erb_index = ERB.new(open('..\src\index.rhtml') {|io| io.read }, nil, '-')
        erb_file = ERB.new(open('..\src\file.rhtml') {|io| io.read }, nil, '-')
		excludes = [/VC98\\Include/i, /Coding\\include\\boost/i, /Coding\\include\\stl/i, /stdafx/i]
		QCov::Profile::html_report 'sample/test1.profile', excludes, erb_index, erb_file
		assert File.exist?('coverage/index.html')
	end
end
